package com.ts.main.mini.controller;

import com.ts.main.mini.dto.WechatPayDto;
import com.ts.main.mini.exception.R;
import com.ts.main.mini.service.WechatPayService;
import com.ts.main.mini.vo.TransactionVo;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:lk
 * @Date: 2025/5/27  00:33
 */
@AllArgsConstructor
@RestController
@RequestMapping("/mini/pay")
@Tag(name = "小程序-微信支付")
public class WechatPayController {

    private final WechatPayService wechatPayService;

    /**
     * 微信下单并生成调起支付的参数
     *
     * @param dto
     * @return
     */
    @Operation(summary = "微信下单并生成调起支付的参数")
    @PostMapping("/generateParams")
    public R<PrepayWithRequestPaymentResponse> generateParams(@RequestBody WechatPayDto dto) {
        return R.ok(wechatPayService.generateParams(dto));
    }

    /**
     * 微信支付异步通知，验证签名并更新订单状态（仅后端使用）。
     * 注：接口放权，支付白名单
     *
     * @param request
     * @return
     */
    @Operation(summary = "微信支付异步通知（仅后端使用）", hidden = true)
    @PostMapping(value = "/notify")
    public R<String> payNotify(HttpServletRequest request) {
        return R.ok(wechatPayService.payNotify(request));
    }

    /**
     * 商户订单号查询订单
     *
     * @param outTradeNo
     * @return
     */
    @Operation(summary = "商户订单号查询订单", hidden = true)
    @PutMapping("/queryOrderByOutTradeNo")
    public R<TransactionVo> queryOrderByOutTradeNo(@Schema(description = "商户订单号")
                                                   @RequestParam("outTradeNo") String outTradeNo) {
        return R.ok(wechatPayService.queryOrderByOutTradeNo(outTradeNo));
    }


    /**
     * 微信支付订单号查询订单(微信支付订单号)
     *
     * @param transactionId
     * @return
     */
    @Operation(summary = "微信支付订单号查询订单", hidden = true)
    @PutMapping("/queryOrderById")
    public R<TransactionVo> queryOrderById(@Schema(description = "微信支付订单号")
                                           @RequestParam("transactionId") String transactionId) {
        return R.ok(wechatPayService.queryOrderById(transactionId));
    }


    /**
     * 微信取消支付（关闭支付订单）
     *
     * @param orderNumber
     * @return
     */
    @Operation(summary = "取消支付（关闭支付订单）", hidden = true)
    @PutMapping("/closeOrder")
    public R closeOrder(@Schema(description = "平台订单号") @RequestParam("orderNumber") String orderNumber) {
        wechatPayService.closeOrder(orderNumber);
        return R.ok();
    }


    /**
     * 微信申请退款
     *
     * @param orderNumber
     * @return
     */
    @Operation(summary = "微信申请退款", hidden = true)
    @PutMapping("/refundOrder")
    public R<Boolean> refundOrder(@Schema(description = "平台订单号") @RequestParam("orderNumber") String orderNumber,
                                  @Schema(description = "退款金额") @RequestParam("totalAmount") String totalAmount) {
        return R.ok(wechatPayService.refundOrder(orderNumber, totalAmount));
    }


    /**
     * 微信退款异步通知（仅后端使用）。
     * 注：接口放权，支付白名单
     *
     * @param request
     * @return
     */
    @Operation(summary = "微信退款异步通知（仅后端使用）", hidden = true)
    @PostMapping(value = "/refundNotify")
    public R refundNotify(HttpServletRequest request) {
        wechatPayService.refundNotify(request);
        return R.ok();
    }
}

